﻿using MESMian.Common;
using MESMian.Common.Helpers;
using MESMian.Context;
using Microsoft.EntityFrameworkCore;
using System.Data;
using System.Windows.Forms;

namespace MESMian
{
    public partial class Login : Form
    {   
        string checkCode =string.Empty; 
        public Login()
        {
            InitializeComponent();
            btnLogin.Click += BtnLogin_Click;
            Load += Login_Load;
            ckbPwd.CheckedChanged += CkbPwd_CheckedChanged;
            txtPwd.KeyPress += TxtPwd_KeyPress;

            pbConfirm.Image = CheckCodeHelper.CenCheckCode(4,pbConfirm.Width,pbConfirm.Height,out checkCode);
            txtConfirm.Text = checkCode;
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void TxtPwd_KeyPress(object? sender, KeyPressEventArgs e)
        {
            // 只允许输入数字（0-9），允许退格键
            if (!char.IsDigit(e.KeyChar) && e.KeyChar != (char)Keys.Back)
            {
                e.Handled = true; // 阻止非数字字符输入
            }
        }

        private void CkbPwd_CheckedChanged(object? sender, EventArgs e)
        {

            if (ckbPwd.Checked)
            {
                // 当复选框被勾选时，显示密码
                txtPwd.PasswordChar = '\0'; // 取消隐藏密码
            }
            else
            {
                // 当复选框未被勾选时，隐藏密码
                txtPwd.PasswordChar = '*';
            }
        }

        private void Login_Load(object? sender, EventArgs e)
        {
            ckbPwd.Checked = false;
            if (!ckbPwd.Checked)
            {
                txtPwd.PasswordChar = '*';
            }

            this.StartPosition = FormStartPosition.CenterScreen;
            // 禁用最大化按钮
            this.MaximizeBox = false;

            // 固定窗体大小，取消调整大小功能
            this.FormBorderStyle = FormBorderStyle.FixedSingle;

        }

        private async void BtnLogin_Click(object? sender, EventArgs e)
        {
            #region 数据判断有无
            if (string.IsNullOrWhiteSpace(txtUser.Text))
            {
                MessageBox.Show("请输入账号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (string.IsNullOrWhiteSpace(txtPwd.Text))
            {

                MessageBox.Show("请输入密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            #endregion



            using (var u = new MyContext())
            {
                var query = await u.Users
                    .Where(user => user.UserName.Equals(txtUser.Text.Trim()))
                    .Where(user => user.Password == txtPwd.Text.Trim())
                    .Select(user => new { user.UserId, user.UserName, user.Status })
                    .SingleAsync(user => user.Status == 0);

                if (query != null)
                {
                    LoginInfo.Id = query.UserId;
                    LoginInfo.UserName = query.UserName;
                    DialogResult = DialogResult.OK;
                }
                else
                {

                }

            }


        }
    }
}
